home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Mosml.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  3.4 KB  |  107 lines  |  [TEXT/Moml]

  1. (* Mosml -- Moscow ML specific functions *)
  2.  
  3. local 
  4.     prim_val argv_   : string Vector.vector = 0 "command_line";
  5.     prim_val byte_   : real -> int -> Word8.word = 2 "get_nth_char";
  6. in
  7.  
  8. fun doubleVec (r : real) =
  9.     Word8Vector.tabulate(8, fn i => byte_ r i)
  10.  
  11. fun floatVec (r : real) = 
  12.     let infix 5 << >> ~>> 
  13.         val dv = doubleVec r 
  14.         val eoffset = (150-23) - (1075-52)
  15.         open Word8 Word8Vector
  16.         val s = andb(sub(dv, 0), 0wx80)
  17.         val e = Int.+(Int.+(Int.*(toInt(andb(sub(dv, 0), 0wx7F)), 0x10),
  18.                             toInt(sub(dv, 1) >> 0w4)),
  19.                       eoffset)
  20.         val e = if Int.<(e, 0) orelse Int.>(e, 255) then 
  21.                     raise Fail "Mosml.floatVec: float range error"
  22.                 else fromInt e
  23.         val m1 = andb(sub(dv, 1), 0wxF)
  24.         val m2 = sub(dv, 2)
  25.         val m3 = sub(dv, 3)
  26.         val m4 = sub(dv, 4)
  27.         val w0 = orb(s, e >> 0w1)
  28.         val w1 = orb(e << 0w7, orb(m1 << 0w3, m2 >> 0w5))  
  29.         val w2 = orb(m2 << 0w3, m3 >> 0w5)
  30.         val w3 = orb(m3 << 0w3, m4 >> 0w5)
  31.     in 
  32.         fromList[w0,w1,w2,w3]
  33.     end
  34.  
  35. prim_val md5sum : string -> string = 1 "md5sum";
  36.  
  37. fun argv () = 
  38.     let fun h i res = 
  39.         if i >= 0 then h (i-1) (Vector.sub(argv_, i) :: res)
  40.         else res
  41.     in h (Vector.length argv_ - 1) [] end;
  42.  
  43. (* Requires Time and Timer to be loaded *)
  44.  
  45. fun time f arg =
  46.     let open Timer
  47.         val cputimer  = startCPUTimer ()
  48.         val realtimer = startRealTimer ()
  49.         fun report () = 
  50.             let val {usr, sys, gc} = checkCPUTimer cputimer;
  51.                 val rea = checkRealTimer realtimer;
  52.                 fun format t = Time.toString t
  53.             in TextIO.print("User: "     ^ format usr ^
  54.                             "  System: " ^ format sys ^ 
  55.                             "  GC: "     ^ format gc  ^ 
  56.                             "  Real: "   ^ format rea ^ "\n")
  57.             end
  58.         fun x before y = x
  59.     in
  60.         (f arg before report ())
  61.         handle e => (report (); raise e)
  62.     end;
  63.  
  64. fun listDir path =
  65.     let open FileSys
  66.         val dir = openDir path
  67.         fun read "" res = res
  68.           | read f  res = read (readDir dir) (f :: res)
  69.         val files = read (readDir dir) []
  70.     in closeDir dir; files end
  71.     handle exn as SysErr (msg, _) => (BasicIO.print msg; raise exn)
  72.  
  73. datatype runresult = 
  74.     Success of string
  75.   | Failure of string
  76.  
  77. fun run cmd args inp =
  78.     let fun catenate xs = 
  79.             String.concat (List.foldr (fn (s, res) => s :: " " :: res) [] xs)
  80.         fun write filename s = 
  81.             let open BinIO
  82.                 val os = openOut filename
  83.             in output(os, s); closeOut os end
  84.         fun read filename = 
  85.             let open BinIO
  86.                 val is  = openIn filename
  87.                 val res = inputAll is
  88.             in closeIn is; res end
  89.         val infile  = FileSys.tmpName ()
  90.         val _ = write infile (Byte.stringToBytes inp)
  91.         val outfile = FileSys.tmpName ()
  92.         val cmdline = 
  93.             catenate (cmd :: List.@(args, ["<", infile, ">&", outfile]))
  94.         val status = Process.system cmdline
  95.         val result = if status = Process.success then 
  96.                          Success (Byte.bytesToString (read outfile))
  97.                      else 
  98.                          Failure (Byte.bytesToString (read outfile))
  99.     in 
  100.         FileSys.remove infile;
  101.         FileSys.remove outfile;
  102.         result
  103.     end
  104.  
  105. end (* local *)
  106.  
  107.